<?php

/**
 * Created by ymk
 * User: Administrator
 * Date: 2018/7/2
 * Time: 16:33
 */

namespace MeetUp\Controllers;

use MeetUp\Model\ActivityCollect;
use MeetUp\Model\UserActivity;
use Validator;
use Illuminate\Http\Request;
use MeetUp\Model\Activity;
use MeetUp\Model\Category;
use MeetUp\Model\Group;
use MeetUp\Model\Topic;
use MeetUp\Model\TopicComment;
use MeetUp\Model\TopicLike;
use MeetUp\Model\UserGroup;

class GroupController extends Controller
{
    /*
    * 分类
    */
    //分类首页
    public function category(Request $request){
        $category = new Category();
        $receive = $request->all();
        $is_hot = isset($receive['is_hot'])?$receive['is_hot']:'';
        //热门分类
        if($is_hot){
            $data = $category->where(['is_hot'=>1])->get()->toArray();
        }else{//全部分类
            $data = $category->get()->toArray();
        }//图片地址
        foreach ($data as $key => $value){
            $data[$key]['category_thumimg'] = $this->url.$value['category_thumimg'];
        }
        return $this->response($data);
    }
    //群组
    public function group(Request $request){
        $group = new Group();
        $user_group = new UserGroup();
        $receive = $request->all();
        $category = isset($receive['categoryid'])?$receive['categoryid']:'';
        //最新小组
        if($receive['state'] == 'new'){
            if($category){
                //具体分类下的最新小组
                $data = $group->where(['category_id'=>$category,'group_via'=>1])
                    ->orderBy('created_at','desc')->take(3)->get()->toArray();
            }else{
                //最新小组
                $data = $group->where(['group_via'=>1])->orderBy('created_at','desc')->take(3)->get()->toArray();
            }

        }//热门小组
        elseif ($receive['state'] == 'hot'){
            if($category){
                //具体分类下的热门小组
                $data = $group->where(['category_id'=>$category,'group_via'=>1,'is_hot'=>1])
                    ->orderBy('updated_at','desc')->take(3)->get()->toArray();
            }else{
                //热门小组
                $data = $group->where(['is_hot'=>1,'group_via'=>1])
                    ->orderBy('updated_at','desc')->take(3)->get()->toArray();
            }
        }
        //精选推荐
        elseif($receive['state'] == 'recommend'){
            if($category){
                //具体分类下的推荐
                $data = $group->where(['category_id'=>$category,'group_via'=>1,'is_recommend'=>1])
                     ->orderBy('updated_at','desc')->take(3)->get()->toArray();
            }else{
                //推荐小组
                $data = $group->where(['is_recommend'=>1,'group_via'=>1])
                    ->orderBy('updated_at','desc')->take(3)->get()->toArray();
            }
        }
        //请求没有数据返回
        if(!count($data)){
            return $this->response([],200,'暂时没有相关的内容');
        }
        //请求有数据
        $data = $this->group_details($data,$receive['uid']);
        //请求有数据返回
        return $this->response($data,200);

    }
    //加入分组
    public function add_group(Request $request){
        $receive = $request->all();
        $user_group = new UserGroup();
        $flag = $user_group->where(['groupid'=>$receive['group_id'],'uid'=>$receive['uid']])->first();
        if($flag){
            return $this->response([],200,'已加入请勿重复加入');
        }else{
            $flag = $user_group->create(['groupid'=>$receive['group_id'],'uid'=>$receive['uid']]);
            if($flag){
                return $this->response([],200,'加入小组成功');
            }else{
                return $this->response([],200,'请稍后再试');
            }
        }
    }
    //分组下的活动与帖子
    public function group_all(Request $request){
        $activity = new Activity();
        $topic = new Topic();
        $group = new Group();
        $user_group = new UserGroup();
        $receive = $request->all();
        $url = $this->url;
        $groupid = $receive['group_id'];
        $uid = $receive['uid'];
        //分组的信息
        $data['group'] = $group->where(['group_id'=>$groupid])
            ->select('group_id','group_name','group_thumimg')->first();
        $data['group']['group_thumimg'] = $this->url.$data['group']['group_thumimg'];
        //是否加入该小组
        $flag = $user_group->where(['groupid'=>$groupid,'uid'=>$uid])->first();
        if($flag){
            $data['group']['is_join'] = 1;
        }else{
            $data['group']['is_join'] = 0;
        }
        //活动的总数
        $data['group']['activity_count'] = $activity->where(['activity_groupid'=>$groupid])->count();
        //帖子的总数
        $data['group']['topic_count'] = $topic->where(['topic_groupid'=>$groupid])->count();
        //分组下的活动
        //热门活动
        $a_hot = $activity->where(['activity_groupid'=>$groupid,'is_hot'=>1,'close'=>1])->get()->toArray();
        //普通活动
        $a_pt = $activity->where(['activity_groupid'=>$groupid,'is_hot'=>0,'close'=>1])->get()->toArray();
        //活动另外的属性
        $data['activity_hot'] = $activity->activity($a_hot,$uid,$url);
        $data['activity_pt'] = $activity->activity($a_pt,$uid,$url);
        //分组下的帖子
        //热门帖子
        $t_hot = $topic->where(['is_hot'=>1,'close'=>1,'topic_groupid'=>$groupid])->get()->toArray();
        $data['topic_hot'] = $this->topic_details($t_hot);
        //普通帖子
        $t_pt = $topic->where(['is_hot'=>0,'close'=>1,'topic_groupid'=>$groupid])->get()->toArray();
        $data['topic_pt'] = $this->topic_details($t_pt);
        return $this->response($data);
    }
    //帖子的详情
    protected function topic_details($data){
        $TopicComment = new TopicComment();
        $TopicLike = new TopicLike();
        foreach ($data as $key => $value){
            //发帖人信息
            $userinfo = Topic::find($value['topic_id'])->user()->get()->take(5)->toArray();
            $data[$key]['user_id'] = $userinfo[0]['id'];
            $data[$key]['user_name'] = $userinfo[0]['name'];
            $data[$key]['thumimg'] = $userinfo[0]['thumimg'];
            //评论数量
            $data[$key]['comment_count'] = $TopicComment->number($value['topic_id']);
            //点赞数量
            $data[$key]['topic_count'] = $TopicLike->number($value['topic_id']);
            //距发帖的时间差(--分钟前)
            $data[$key]['time'] = $this->formatDate($value['created_at']);
            //图片路径转换
            $data[$key]['topic_img'] = $this->url.$value['topic_img'];
        }
        return $data;
    }
    //分类的详情
    protected function group_details($data,$uid){
        foreach ($data as $key => $value){
            $user_group = new UserGroup();
            //是否加入该小组
            $flag = $user_group->where(['groupid'=>$value['group_id'],'uid'=>$uid])->first();
            if($flag){
                $data[$key]['is_join'] = 1;
            }else{
                $data[$key]['is_join'] = 0;
            }
            //该分组有多少成员
            $data[$key]['count'] = $user_group->where(['groupid'=>$value['group_id']])->count();
            //图片地址
            $data[$key]['group_thumimg'] = $this->url.$value['group_thumimg'];
        }
        return $data;
    }

    //创建聚会
    public function add_activity(Request $request){
        $activity = new Activity();
        $receive = $request->all();
        //验证数据
        $flag = $activity->inspect($request);
        if($flag){return $this->response($flag);}
        $data = [
            'uid' => $receive['uid'],
            'activity_name' => $receive['name'],
            'E' => $receive['Lat'],
            'N' => $receive['Lng'],
            'start_time' => strtotime($receive['start_time']),
            'end_time' => strtotime($receive['end_time']),
            'time_supply' => $receive['area'],
            'details' => $receive['details'],
            'activity_groupid' => $receive['group_id'],
        ];
        //添加聚会入库
        $flag = $activity->create($data);
        if($flag){
            return $this->response([],200,'新增成功');
        }else{
            return $this->response([],200,'稍后再试');
        }
    }
    //修改活动
    public function edit_activity(Request $request){

    }
    //活动详情
    public function activity_details(Request $request){
        $activity = new Activity();
        $receive = $request->all();
        /*//判断权限
        $flag = $activity->where(['activity_id'=>$receive['activity_id'],'uid'=>$receive['uid']])
            ->select('uid')->first();
        if(!$flag){
            $this->response([],200,'没有权限');
        }*/
        $result = $activity->where(['activity_id'=>$receive['activity_id'],'uid'=>$receive['uid']])->first();
        if($result){
            return $this->response($result);
        }else{
            return $this->response([],200,'稍后再试');
        }
    }


    /*
    *个人中心
    */
    //加入的群组
    public function user_group(Request $request){
        $receive = $request->all();
        $user_group = new UserGroup();
        //加入的群组的ID
        $result = $user_group->where(['uid'=>$receive['uid']])->orderBy('created_at','desc')->get();
        if(!count($result)){
            return $this->response([],200,'没有加入任何群组');
        }
        //加入的群组的详情
        foreach ($result as $key => $val){
            $groupinfo[$key] = UserGroup::find($val->groupid)->group()
                ->select('group_id','group_name','group_thumimg')
                ->first();
            //加入的时间
            $groupinfo[$key]->created_at = $val->created_at;
            //群组的图片
            $groupinfo[$key]->group_thumimg = $this->url.$groupinfo[$key]->group_thumimg;
            //加入的时间是否超过一个月
            if((time() - strtotime($val->created_at)) < 2592000){
                $groupinfo[$key]->state = 1;//加入时间不到一个月
            }else{
                $groupinfo[$key]->state = 0;//加入时间超过一个月
            }

        }
        return $this->response($groupinfo);
    }
    //我的收藏
    public function collect(Request $request){
        $collect = new ActivityCollect();
        $activity = new Activity();
        $receive  = $request->all();
        $url = $this->url;
        $uid = $receive['uid'];
        //收藏的活动的ID
        $result = $collect->where(['uid'=>$uid])->get()->toArray();
        foreach ($result as $key => $val){
            //收藏的活动的具体内容
            $activityinfo = $activity->where(['activity_id'=>$val['activityid']])->get()->toArray();
            $data[$key] = $activity->activity($activityinfo,$uid,$url);
        }
        //合并数组
        $nin = [];
        foreach($data as $val){
            foreach ($val as $k){
                $nin[] = $k;
            }
        }
        return $this->response($nin);
    }
    //我的创建
    public function found(Request $request){
        $group = new Group();
        $activity = new Activity();
        $topic = new Topic();
        $receive = $request->all();
        $uid = $receive['uid'];
        $url = $this->url;
        //创建的分组
        $result['group'] = $group->where(['start_uid'=>$uid])->get();
        $data['group'] = $this->group_details($result['group'],$uid);
        //发起的活动
        $result['activity'] = $activity->where(['uid'=>$uid])->get()->toArray();
        foreach ($result['activity'] as $key => $val){
            $data['activity'] = $activity->activity($result['activity'],$uid,$url);
        }
        //发的帖子
        $result['topic'] = $topic->where(['uid'=>$uid])->get()->toArray();
        $data['topic'] = $this->topic_details($result['topic']);
        return $this->response($data);
    }
    //活动聚会提醒
    public function news(Request $request){
        $receive = $request->all();
        $uid = $receive['uid'];
        $user_activity = new UserActivity();
        $activity = new Activity();
        //参与的活动ID
        $result = $user_activity->where(['m_uid'=>$uid])->get();//return $result;
        if(count($result)){
            //参与的即将开始的活动
            foreach ($result as $key => $val){
                $data = $activity->where(['activity_id'=>$val['m_activityid']])
                    ->orWhere('start_time','>',time())
                    ->orderBy('start_time','asc')
                    ->get()->toArray();
            }
            if(count($data)){
                foreach ($data as $key => $val){
                    //检查是否在开始活动前八个小时
                    if($val['start_time'])
                    //图片路径
                    $data[$key]['activity_img'] = $this->url.$data[$key]['activity_img'];
                    //时间转换
                    $time = $data[$key]['start_time'];
                    $data[$key]['start_time'] = date("Y-m-d H:i:s",$time);
                    //$data[$key]['remind_time'] = date('H:i:s',($data[$key]['start_time']-28800));
                }
                return $this->response($data);
            }else{
                return $this->response([],200,'暂时没有消息');
            }
        }else{
            return $this->response([],200,'没有参与活动');
        }

    }
    //删除收藏
    public function delete_collect(Request $request){
        $collect = new ActivityCollect();
        $receive = $request->all();
        $uid = $receive['uid'];
        $acivityid = $receive['activity_id'];
        //检测数据是否存在
        $result = $collect->where(['uid'=>$uid,'activityid'=>$acivityid])->first();
        if($result){
            //若存在则删除
            $flag = $collect->where(['uid'=>$uid,'activityid'=>$acivityid])->delete();
            if($flag == true){
                return $this->response([],200,'删除成功');
            }else{
                return $this->response([],200,'删除失败');
            }
        }else{
            return $this->response([],200,'已经删除');
        }

    }
    //删除我的创建
    public function delete_found(Request $request){
        $receive = $request->all();
        $type = $receive['type'];
        $id = $receive['id'];
        $uid = $receive['uid'];
        $group = new Group();
        $activity = new Activity();
        $topic = new Topic();
        //删除创建的分组
        if($type == 'group'){
            $result = $group->where(['start_uid'=>$uid,'group_id'=>$id])->first();
            if($result){
                //删除分组下的活动
                $activity_flag = $activity->where(['activity_groupid'=>$result->group_id])->delete();
                //删除活动下的成员

                //删除分组下的帖子
                $topic_flag = $topic->where(['topic_groupid'=>$result->group_id])->delete();
                //删除分组
                $group_flag = $group->where(['start_uid'=>$uid,'group_id'=>$id])->delete();
                if($activity_flag && $topic_flag && $group_flag){
                    return $this->response([],200,'删除成功');
                }
            }else{
                return $this->response([],200,'没有找到要删除的组');
            }

        }//删除创建的活动
        elseif($type == 'activity'){
            $flag = $activity->where(['uid'=>$uid,'activity_id'=>$id])->delete();
        }//删除帖子
        elseif($type == 'topic'){
            $flag = $topic->where(['uid'=>$uid,'topic_id'=>$id])->delete();
        }
        if($flag == true){
            return $this->response([],200,'删除成功');
        }else{
            return $this->response([],200,'稍后再试');
        }

    }

    //创建分组
    public function add_found(Request $request){
        $receive = $request->all();
        $img = $receive['img'];//图片的base64
        $group = new Group();
        //验证数据
        $this->inspect($request);
        //上传照片
        $qiniu = new \MeetUp\Services\qiniu;
        foreach ($img as $key => $val){
            $result = $qiniu->qiniu($val,'group'); //使用七牛云上传
            if($result){
                $thumimg[] = $result;
            }else{
                return $this->response([],200,'上传出错');
            }

        }

        $data = [
            'group_name' => $receive['name'],
            'start_uid' => $receive['uid'],
            'manage_uid' => $receive['uid'],
            'group_thumimg'=>$thumimg[0],
            'group_content' => $receive['group_content'],
            'category_id'=>$receive['category_id']
        ];
        //添加数据入库
        $flag = $group->create($data);
        if($flag){
            return $this->response([],200,'ok,等待审核');
        }else{
            return $this->response([],200,'稍后再试');
        }
    }
    //修改分组
    public function edit_group(Request $request){
        $receive = $request->all();
        $img = isset($receive['img'])?$receive['img']:'';//图片的base64
        $group = new Group();
        //验证数据
        $this->inspect($request);
        //上传照片
        $qiniu = new \MeetUp\Services\qiniu;
        if($img){
            foreach ($img as $key => $val){
                $result = $qiniu->qiniu($val,'group'); //使用七牛云上传
                if($result){
                    $thumimg[] = $result;
                }else{
                    $this->response([],200,'上传出错');
                }
            }
            $data['thumimg'] = $thumimg[0];
        }
        $data = [
            'group_name' => $receive['name'],
            'group_content' => $receive['group_content'],
            'category_id'=>$receive['category_id']
        ];
        //修改数据
        $data['group_via'] = 0;
        $flag = $group->where(['group_id'=>$receive['group_id']])->update($data);
        if($flag){
            return $this->response([],200,'ok,等待审核');
        }else{
            return $this->response([],200,'稍后再试');
        }
    }
    //验证分组的数据
    public function inspect($request){
        //验证数据
        $message=[
            'name.required'=>'标题不能空',
            'uid.required'=>'创建人不能为空',
            'category_id.required'=>'所属分类不能为空',
            'details.required'=>'内容不能为空'
        ];
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'uid' => 'required',
            'category_id' => 'required',
            'details' => 'required',
        ],$message);
        if ($validator->fails()) {
            $data = $validator->errors()->all();
            return $this->response($data);
        }
    }
    //分组的详情
    public function groupinfo(Request $request){
        $receive = $request->all();
        $group = new Group();
        //根据ID查询某条具体值
        $groupinfo = $group->where(['group_id'=>$receive['group_id']])->first()->toArray();
        if($groupinfo){
            $groupinfo['group_thumimg'] = $this->url.$groupinfo['group_thumimg'];
            return $this->response($groupinfo,200);
        }else{
            return $this->response([],200,'稍后再试');
        }


    }

}








